package action;

import java.util.Map;
import java.util.Set;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import biz.MusicBiz;
import biz.impl.MusicBizImpl;
import entity.return_type.DataByPaging;
import entity.return_type.DataNoPaging;
import util.HttpIOUtil;
import util.MusicUtil;
import util.baidu.BaiduMusicUtil;

public class MusicAction extends SuperAction {

	/**
	 * 音乐相关动作
	 * @anthor billdonwey
	 */
	private static final long serialVersionUID = 1L;
	private static final MusicBiz MUSIC_BIZ = new MusicBizImpl();
	
	// 收集歌曲信息
	public void collectorMusicInfo() {
		Integer sum = params.getInteger("sum");
		String type = params.getString("type");
		DataNoPaging out = new DataNoPaging();
		out.setData(MusicUtil.collectorMusicInfo(sum, type));
		out.setMsg("抓取成功");
		HttpIOUtil.ObjectToWeb(response, out);
	}
	
	// 获取播放列表
	public void playerList() {
		DataNoPaging out = new DataNoPaging();
		Integer userId = params.getInteger("userId");
		out.setData(MUSIC_BIZ.playerList(userId));
		out.setMsg("当前获取用户播放列表");
		HttpIOUtil.ObjectToWeb(response, out);
	}
	
	// 获取歌曲列表
	public void musicList() {
		DataByPaging out = new DataByPaging();
		String type = params.getString("type");
		Integer firstIndex = params.getInteger("firstIndex");
		Integer maxSums = params.getInteger("maxSums");
		out.setData(MUSIC_BIZ.musicList(type, firstIndex, maxSums));
		HttpIOUtil.ObjectToWeb(response, out);
	}
	
	// 获取歌曲类型
	public void musicType() {
		DataByPaging out = new DataByPaging();
		JSONArray list = new JSONArray();
		Map<String, String> MUSIC_TYPE = BaiduMusicUtil.MUSIC_TYPE;
		Set<String> keys = MUSIC_TYPE.keySet();
		for (String key : keys) {
			JSONObject j = new JSONObject();
			j.put("id", key);
			j.put("name", MUSIC_TYPE.get(key));
			list.add(j);
		}
		out.setData(list);
		HttpIOUtil.ObjectToWeb(response, out);
	}
}
